<template>
	<view class="viewcontent">
		<view class="viewcontenthead">
			<view class="fl" @click="$UTILS.goBack(1)"></view>
			<view class="fr" style="color: #fdf5fa;"> 装箱校验</view>
			<view class="leftitem" ></view>
		</view>
		<view class="viewconten">
			 <view class="viewcontentitle">
				 <u-button @click="initpdalist">开始识别</u-button>
			 </view>
			 <view class="backlistview">
			 	<view class="viewcontenmes" v-for="(item,index) in this.itemlist" :key="index">
			 					 <view class="contenmesitems">
			 						 <view class="titlemes">箱信息</view>
			 						 <view class="boxname">箱名称：{{index}}</view>
			 						 <view class="boxcode">箱编码：{{item.equipmentList[0].equipmentCode}}</view>
			 						 <view class="boxitems" v-for="(ite,indexd) in item.equipmentList " :key="indexd">
			 							 <view class="wrapitem">
			 							 		<view class="contenmesbox">
			 							 			<view class="contenmesboxtitle"><view>序号：{{indexd+1}}</view></view>
			 							 			<view class="contenmesboxtitle"><view>物资名称：{{ite.equipmentName}}</view></view>
			 							 			<view class="contenmesboxtitle"><view>物资编码：{{ite.equipmentCode}}</view></view>
													<view class="contenmesboxtitle"><view>状态：{{ite.equipmentStatus==0?'缺失':ite.equipmentStatus==1?'正常':'任务外'}}</view></view>
			 							 			<view class="contenmesboxtitle"><view>RFID：{{ite.equipmentRfid}}</view></view>
			 							 		</view> 
			 							 </view>
			 						 </view>
			 					 </view> 
			 	</view>				 
			 </view>
			 
		</view>
		<keyboard-listener @keydown="onKeydown"></keyboard-listener>
		<keyboard-listener @keyup="onKeyup"></keyboard-listener>
		<myLoading v-if="loading" style="position: absolute;z-index: 9;"></myLoading>
	</view>
</template>

<script>
	import keyboardListener from '../../../components/uniapp-keyboard-listener/src/keyboard-listener.vue'
	// #ifdef APP-PLUS
		// const pda = uni.requireNativePlugin("PDAModule");
		// const tone = uni.requireNativePlugin("ToneModule");
		// var log = uni.requireNativePlugin("LogModule")
	// #endif
	let andoridModule = uni.requireNativePlugin('seuic-uhf');
		export default {
	    components: {
			keyboardListener
			},
		onLoad() {
						let item = this.$UTILS.getitemlist().filter(item=>{return item.taskCode == this.$UTILS.gettaskCode() })
									let are =  item[0].equipmentList.reduce((arr,cur)=>{
										let boxname = cur.boxName || '未知箱组'
										if(!arr[boxname]){
											arr[boxname]={}
											arr[boxname].equipmentList=[]
										}
										cur.equipmentStatus = 0
										arr[boxname].equipmentList.push(cur)
										return arr
									},{})
						this.itmelistcopy = JSON.parse(JSON.stringify(are)) 
						console.log(this.itmelistcopy)
		},
		data() {
			return {
				itmelistcopy:[],
				itemlist:[],
				rfidList:[],
				lastQueryTimestamp:0,
				loading:false,
				currentkey:''
			};
		},
		watch:{
		},
		methods:{
			putscreen(){
				setTimeout(()=>{
					if(this.loading){
						this.loading = false
					}
					
				},5000)
				
			},
			pushitem(){
				// 遮罩删除
				// this.putscreen()
				let boxfid = []
				for(let item in this.itmelistcopy){
					this.itmelistcopy[item].equipmentList.forEach(item=>{
						boxfid.push(item.boxRfid)
						if(this.rfidList.indexOf(item.boxRfid)!= -1){
							this.currentkey = item.boxName
						}
					})
				}
				// console.log(this.rfidList)
				this.rfidList = this.rfidList.filter(item=>{
					return item != this.itmelistcopy[this.currentkey]?.equipmentList[0].boxRfid
				})
				// gai
				for(let ite in this.itmelistcopy){
					this.itmelistcopy[ite].equipmentList.map((item,index)=>{
						if(this.rfidList.indexOf(item.equipmentRfid)!= -1){
							item.equipmentStatus = 1   
							// this.rfidList = this.rfidList.filter(ite=>{
							// 	return ite != item.equipmentRfid
							// })
						}else{
							item.equipmentStatus = 0
						}
						return item
					})
				}
				
				
				
				// this.itmelistcopy[this.currentkey].equipmentList.map((item,index)=>{
				// 	if(this.rfidList.indexOf(item.equipmentRfid)!= -1){
				// 		item.equipmentStatus = 1   
				// 		this.rfidList = this.rfidList.filter(ite=>{
				// 			return ite != item.equipmentRfid
				// 		})
				// 	}else{
				// 		item.equipmentStatus = 0
				// 	}
				// 	return item
				// })
				
				// gai
				boxfid = [ ...new Set(boxfid)]
				this.rfidList = this.rfidList.filter(item=>{
					return  !boxfid.includes(item)
				})
				
				
				this.itemlist = JSON.parse(JSON.stringify(this.itmelistcopy)) 
				if(this.rfidList.length>0){
					this.rfidList.forEach(item=>{
						let obj = {}
						obj.equipmentName = '未知设备'
						obj.equipmentCode = '未知编码'
						obj.equipmentStatus = 2
						obj.equipmentRfid = item
						this.itemlist[this.currentkey]?.equipmentList.push(obj)
						
					})
				}
				
				this.loading = false
			},
			initpdalist(){
				this.loading = false
				this.itemlist = {}
			},
			 onKeydown(e) {
				 this.loading = true
			 		const now = Date.now();
			 		 // 节流间隔，1000 毫秒
			 		 const throttleDuration = 3000;
			 		 if (now - this.lastQueryTimestamp < throttleDuration) {
			 		     // 如果当前时间与最后一次执行的时间差小于节流间隔，则不执行
						 this.loading = false  
			 		     return;
			 		 }
					 // this.rfidList = []
					 // this.itemlist = []
					 
			 		 this.lastQueryTimestamp = now;
			 			andoridModule.inventoryStart((e) => {
			 				if (e != '未寻到') {
			 					let jsonResult = JSON.parse(JSON.stringify(e));
			 					jsonResult.forEach(item =>{
			 						if (this.rfidList.indexOf(item.id) == -1) {
			 							 this.rfidList.push(item.id)
			 						}						
			 					})
								this.pushitem()
			 				} else {
			 					this.templedata = e
			 				}
			 			});
			 },
			 onKeyup(e) {
			 		andoridModule.inventoryStop((e) => {
			 			this.templedata = e
			 		});
			 		this.loading = false
			 		
			 		
			 },
			  processData(result) {
			 	let jsonResult = JSON.parse(result);
			 	if (this.rfidList.indexOf(jsonResult.epc) == -1) {
			 		 this.rfidList.push(jsonResult.epc)
			 	}						
			 },
		}
	}
</script>
<style lang="scss">
 .viewcontent{
	 display: flex;
	 flex-direction: column;
	 width: 100%;  
	 height: 100vh; 
	 box-sizing: border-box;
	 padding-top: 55rpx;
	   .viewcontenthead{
	   		 width: 100%;
	   		 height: 5%;
	   		 display: flex;
	   		 justify-content: space-between;
	   		 align-items: center;
	   		 .fl{
	   			 width: 100rpx;
	   			 height: 10vh;
	   			 line-height: 10vh;
	   			 background: url("/static/right-arrow.png") no-repeat center;
	   			 background-size: 14%;
	   			 transform: rotate(180deg);
	   			 cursor: pointer; // 增加鼠标指针样式
	   		 }
	   		 .fr{
	   			 cursor: pointer; // 增加鼠标指针样式
	   		 }
	   		 .leftitem{
	   			 width: 100rpx;
	   			 display: flex;
	   			 justify-content: center;
	   			 z-index: 999;
	   		 }
	   } 
	   .viewconten{
		   width: 100%;
		   height: 95%;
		   min-height: 89%;
		   box-sizing: border-box;
		   margin-top: 15rpx;
		   
			  .viewcontentitle{
			  			    height: 100rpx;
			  				width: 100%;
			  				display: flex;
			  				flex-direction: column;
			  				justify-content: space-between;
			  				button{
			  					width: 80%;
			  				}
			    }
				.backlistview{
					height: calc(100% - 160rpx);
					width: 100%;
					overflow: auto;
					
					.viewcontenmes{
						width: 98%;
						margin: 30rpx  auto 0;
						border: 1px solid #FFFFFF;
						border-radius: 15px;
						background-color:rgba(47, 40, 83, 0.8) ;
						font-family:PingFang SC;
						font-weight:550;
						color:rgba(250, 249, 249, 0.8);
						font-size:16px;
						box-sizing: border-box;
						padding: 24rpx;
						.contenmesitems{
							width: 100%;
							.titlemes{
								line-height: 2;
								background-color:rgba(47, 40, 83, 0.8) ;
								font-family:PingFang SC;
								font-weight:550;
								color:rgba(161, 160, 160, 0.8);
								font-size:24px;
							}
							.boxname{
								width: 100%;
								height: 50rpx;
							}
							.boxcode{
								width: 100%;
								height: 50rpx;
							}
							.boxitems{ 
								border: 1px solid #FFFFFF;
								border-radius: 15px;
								background-color:rgba(31, 27, 56, 0.8) ;
								box-sizing: border-box;
								margin: 10rpx  auto ;
								width: 100%;
								height: 250rpx;
								.wrapitem{
									width: 100%;
									height: 100%;
									.contenmesbox{
										width: 100%;
										height: 100%;
										.contenmesboxtitle{
										 width: 100%;
										 height: 20%;
										 box-sizing: border-box;
										 padding-left: 15rpx;
										 view{
										 text-overflow: ellipsis;
										 overflow: hidden;
										 white-space: nowrap;
										    	}
										 }
									}
									
								}
								
							 }
							.contenmesbox:last-child{
									margin: 0;
							}
						}
					}
				}
				
		   } 
	}
</style>